const {
    wechatAPIS
} = require('../tools/wechatApi');
const {
    Mysql,
    TripleEncrypt
} = require('../tools');
module.exports = async ctx => {
    let {
        openid,
        uid
    } = ctx.query;
    try {
        if (!openid || !uid) {
            ctx.state.data = {
                code: '002',
                msg: '参数确实'
            }
        } else {
            let sql = `SELECT * FROM vip WHERE openId = '${openid}'`;
            let res = await Mysql(sql);
            let userInfo = await wechatAPIS.getBaseInfo(openid);
            if (!res.length) {
                let data = TripleEncrypt(JSON.stringify({
                    uid,
                    ...userInfo
                }))
                ctx.state.data = {
                    code: '001',
                    data
                }
                return;
            } else {
                let now = Date.now();
                if (now >= res[0].endTime) {
                    res[0].isVip = 0
                }
                let data = TripleEncrypt(JSON.stringify({
                    ...res[0],
                    ...userInfo
                }))
                ctx.state.data = {
                    code: '001',
                    data
                }
            }
        }
    } catch (error) {
        ctx.state.data = {
            code: '002',
            msg: '内部错误'
        }
    }
}